package com.contexticon.filehandling;

import java.io.BufferedReader;
import java.io.Console;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.PatternSyntaxException;

public class RegexTestHarness2 {

	public static void main(String[] args){
		Pattern pattern = null;
		Matcher matcher = null;

		String strFilePath = "Spanien.srt";
		String fileContents = "";
		// reading in the file
		try {
			fileContents = readFileAsString(strFilePath);
			//System.out.println(fileContents);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// find utterances in the file contents
		System.out.println("Matching the pattern");
		try{
			final String nl = System.getProperty("line.separator");
			final String sp = "[\\t]";

			// regular expression examples and tutorials
			// http://www.vogella.com/articles/JavaRegularExpressions/article.html
	

			pattern = Pattern.compile("\\d+"+ nl
					+ "(\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d)" 
					+ "\\s-->\\s" +"(\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d)"  +nl 
					+ "([\\w\\s,]+)" + nl 
					);
					//+ "(<meta>"
					//+ "(<uncertainty>(\\d)</uncertainty>)?"
					//+ "(<speaker>([\\w\\s]+)</speaker>)?"
					//+ "(<fidelity>(\\d)</fidelity>)?"
					//+ "(<languageTags>([\\w\\s,]+)</languageTags>)?"
					//+ "(<contentTags>([\\w\\s,]+)</contentTags>)?"
					//+ "(<transferTags>([\\w\\s,]+)</transferTags>)?"
					//+ "</meta>)?"
					//+ ".*" + nl					                       
					//);   
			System.out.println("Pattern compiled");
			
			matcher = pattern.matcher(fileContents);
		}
		catch(PatternSyntaxException pse){
			System.out.println("No utterances found");
		}

		// loop over the utterances found
		boolean found = false;
		int noMatches = 0;
		while (matcher.find()) {
			float startTimeMs = Float.parseFloat(matcher.group(1))*3600*1000 +  Float.parseFloat(matcher.group(2))*60*1000 +  Float.parseFloat(matcher.group(3))*1000 + Float.parseFloat(matcher.group(4));
			float endTimeMs =  Float.parseFloat(matcher.group(5))*3600*1000 +  Float.parseFloat(matcher.group(6))*60*1000 +  Float.parseFloat(matcher.group(7))*1000 +  Float.parseFloat(matcher.group(8));                

			System.out.println(startTimeMs);
			System.out.println(endTimeMs);               
			System.out.println(matcher.group(9));
			//System.out.println("uncertainty: " + matcher.group(14));
			//System.out.println("speaker: " + matcher.group(16));
			//System.out.println("fidelity: " + matcher.group(18));
			//System.out.println("languageTags: " + matcher.group(20));
			///System.out.println("contentTags: " + matcher.group(22));
			//System.out.println("transferTags: " + matcher.group(24));
			found = true;
			noMatches++;
		}
		System.out.println("No matches "+ noMatches);
		if(!found){
			System.out.println("No match found.%n");
		}
		
		
		// read the scene starts
		try{

			pattern = Pattern.compile("<sceneStart>(\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d)</sceneStart>");   			
			matcher = pattern.matcher(fileContents);
		}
		catch(PatternSyntaxException pse){
			System.out.println(pse.toString());
		}
		// loop over all scene starts
		while (matcher.find()) {
			//System.out.println(                    matcher.group());
			float sceneStart = Float.parseFloat(matcher.group(1))*3600*1000 +  Float.parseFloat(matcher.group(2))*60*1000 +  Float.parseFloat(matcher.group(3))*1000 + Float.parseFloat(matcher.group(4));
			//System.out.println(sceneStart);

		}
		if(!found){
			System.out.println("No match found.%n");
		}
		
		
		
		
		
		
		
	}

	// reads a text file from disk
	static String readFileAsString(String filePath)
			throws java.io.IOException{
		StringBuffer fileData = new StringBuffer(1000);
		BufferedReader reader = new BufferedReader(
				new FileReader(filePath));
		char[] buf = new char[1024];
		int numRead=0;
		while((numRead=reader.read(buf)) != -1){
			String readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
			buf = new char[1024];
		}
		reader.close();
		return fileData.toString();
	}


}

